/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sais.eswn.card;

import java.util.ArrayList;
import java.util.ListIterator;
import org.sais.eswn.tool.Tools;

/**
 *
 * @author Romulus
 */
public class CardSet<T extends Card> extends ArrayList<Card> {

    public static CardSet emptySet() {
        return new CardSet();
    }

    public CardSet() {
    }

    public CardSet(ArrayList<String> cardnos) {
        this.clear();
        for (String no : cardnos) {
            this.add(Tools.getCard(no));
        }
    }

    public boolean contains(String cardno) {
        ListIterator<Card> it = this.listIterator();
        while (it.hasNext()) {
            if (it.next().no.equals(cardno)) {
                return true;
            }
        }
        return false;
    }

    public Card pop(String cardno) {
        for (int i = 0; i < this.size(); ++i) {
            if (this.get(i).no.equals(cardno)) {
                Card ret = this.get(i);
                this.remove(i);
                return ret;
            }
        }
        return null;
    }

    @Override
    public boolean add(Card e) {
        return super.add(e);
    }

    public int counts(String cardno) {
        int rtn = 0;
        ListIterator<Card> it = this.listIterator();
        while (it.hasNext()) {
            if (it.next().no.equals(cardno)) {
                rtn++;
            }
        }
        return rtn;
    }

    public String listCardnosForSock() {
        String ret = "";
        for (Card c : this) {
            ret += c.no + " ";
        }
        if (ret.length() > 0) {
            return ret.substring(0, ret.length() - 1);
        } else {
            return "";
        }
    }
}
